package jdbcmapper;

import java.util.Vector;

import lotus.domino.*;
/**
 * <p>Title: JDBC Mapper</p>
 *
 * <p>Description: Libraries to copy documents form Lotus Notes to jdbc
 * table</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: The Salvation ARmy</p>
 *
 * @author Ansel Robateau
 * @version 1.1
 */
public class Mapping {
    private int index = 0;
    private Vector<NotesDB> m_subs;
    private Database m_db;
    private Session m_session;
    public static final int INSTANT = 0;
    public static final int INTERMEDIATE = 1;
    public static final int HOURLY = 2;
    public static final int DAILY = 3;
    public static final int WEEKLY = 4;
    public static final int MONTHLY = 5;
    private DBMapper m_parent;
    public Mapping(DBMapper dbm,Session session,Database db) {
        this.m_parent = dbm;
        this.m_session = session;
        this.m_db = db;
        this.m_subs = new Vector<NotesDB>();
    }
    public DBMapper getParent(){
        return this.m_parent;
    }
    public NotesDB getFirstDB(){
        return (NotesDB)this.getFirst();
    }
    public NotesDB getNextDB(){
        return (NotesDB)this.getNext();
    }
    public void LoadMapping() throws Exception {
        View view = this.m_db.getView("Notes Databases");
        ViewEntryCollection vc = view.getAllEntries();
        ViewEntry entry = vc.getFirstEntry();
        Document doc;
        while(entry != null){
            doc = entry.getDocument();
            NotesDB notes_database = new NotesDB(this.m_session,doc);
            this.m_subs.add(notes_database);
            notes_database.LoadMapping(this);
            entry = vc.getNextEntry();
        }
    }
    private Object getFirst(){
        this.index = 0;
        if(this.m_subs.size() < 1){
            return null;
        }else{
            return this.m_subs.get(0);
        }
    }
    private Object getNext(){
        this.index++;
        if(this.m_subs.size() < this.index+1){
            return null;
        }else{
            return this.m_subs.get(this.index);
        }
    }

    /**
     * ExportDocuments
     */
    public void ExportDocuments(int priority) throws Exception {
        NotesDB db = this.getFirstDB();
        while(db != null){
            NotesForm form = db.getFirstForm();
            while(form != null){
                SQLDB sqldb = form.getFirstSQLDB();
                while(sqldb != null){
                    Table table = sqldb.getFirstTable();
                    while(table != null){
                    	try {
                            Integer p = null;
                            try{
                                p = new Integer(table.getString(Table.
                                        int_PRIORITY));
                                //System.out.println("Priority for "+ table.getString(table.SQLTABLE)+" = " + p.toString());
                            }catch(Exception e){
                                //There is no priority set for this table
                                //System.out.println("Priority for " + table.getString(table.SQLTABLE)+" = nothing");
                            }
                            if (p != null && p.intValue() == priority) {
                                table.ExportDocuments();
                                
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        table = sqldb.getNextTable();
                    }
                    sqldb = form.getNextSQLDB();
                }
                form = db.getNextForm();
            }
            db = this.getNextDB();
        }
    }

    public Vector getM_log() {
        return this.getParent().getM_log();
    }
	public void UnloadMapping() {
		NotesDB db = this.getFirstDB();
        while(db != null){
            NotesForm form = db.getFirstForm();
            while(form != null){
                SQLDB sqldb = form.getFirstSQLDB();
                while(sqldb != null){
                    Table table = sqldb.getFirstTable();
                    while(table != null){
                        try {
                            table.m_subs.clear();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        table = sqldb.getNextTable();
                    }
                    sqldb.m_subs.clear();
                    sqldb = form.getNextSQLDB();
                }
                form.m_subs.clear();
                form = db.getNextForm();
            }
            db.m_subs.clear();
            db = this.getNextDB();
        }
        this.m_subs.clear();
	}
}
